// information/autoReceJJK/index.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    console.log('自动领取玖玖卡页面', options);
    let params = decodeURIComponent(options.scene);
    if (params && params != 'undefined') {//扫描小程序二维码进入
      let plist = '';
      if(params.indexOf('p-') != -1){
        let param = params.match(/p-(\S*)/)[1];//参数
        plist = param.split('_');
      }else{
        let param = params.match(/p=(\S*)/)[1];//参数
        plist = param.split('#');
      }
      wx.setStorageSync('cardId', plist[0]);
      that.receiveJJK(plist[2], plist[1], plist[3], plist[4]);
    } else {
      that.receiveJJK(options.createUid, options.cardItemId, options.timestamp, options.source);
    }
  },
  receiveJJK: function (createUid, cardItemId, timestamp, source) {
    let that = this;
    wx.showLoading({
      title: '正在领取...',
      mask: true
    })
    if (app.globalData.userInfo) {
      wx.request({
        url: app.globalData.host + '/api/Coupon/ReceiveCard?uid=' + app.globalData.userInfo.Uid + '&saleUid=' + createUid + '&cardItemId=' + cardItemId + '&code=' + parseInt(timestamp) + '&source=' + source,
        success: function (res) {
          wx.hideLoading();
          if (res.data.Code == 0) {
            wx.showToast({
              title: '领取成功',
              mask: true
            })
            wx.reLaunch({
              url: '/merchant/merchants/myCardCoupon/index?tagindex=5&showWxcard=1',
            })
          } else {
            wx.showToast({
              icon: 'none',
              title: res.data.Message,
              duration: 1500,
              mask: true
            })
            setTimeout(function () {
              wx.reLaunch({
                url: '/merchant/merchants/myCardCoupon/index',
              })
            }, 2000)
          }
        }
      })
    } else {
      setTimeout(function () {
        that.receiveJJK(createUid, cardItemId, timestamp, source);
      }, 1000)
    }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  }
})
